package com.itheima.druid;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class DruidDemo {
    public static void main(String[] args) throws Exception {
        //1.导入jar包
        //2.定义配置文件
        //3.加载配置文件
        Properties prop = new Properties();
        String path = ClassLoader.getSystemClassLoader().getResource("druid.properties").getPath();
        System.out.println("path = " + path);

        prop.load(new FileInputStream(path));
        //4.获取数据库连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        //5.获取连接
        Connection connection = dataSource.getConnection();
        System.out.println("connection = " + connection.getClass());

        PreparedStatement prsmt = connection.prepareStatement("select * from student");
        ResultSet rs = prsmt.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }

        //释放资源
        rs.close();
        prsmt.close();
        connection.close();

    }
}
